﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AllTest.Tools;
using System.Threading;

namespace AllTest.MainForm
{
    public partial class FrmBackGroundWorker : Form
    {
        public FrmBackGroundWorker()
        {
            InitializeComponent();            
            back.WorkerReportsProgress = true;
            back.WorkerSupportsCancellation = true;
            back.ProgressChanged += new ProgressChangedEventHandler(back_ProgressChanged);
            back.RunWorkerCompleted += new RunWorkerCompletedEventHandler(back_RunWorkerCompleted);            
            back.DoWork += new DoWorkEventHandler(back_DoWork);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            DelegateImpl impl = new DelegateImpl();
            DelegateClass.IntegerDelegate intDele = new DelegateClass.IntegerDelegate(impl.IntegerDelegate);            
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            try
            {
                btnStart.Enabled = false;
                btnCancel.Enabled = true;

                if (Thread.CurrentThread.IsThreadPoolThread)
                {
                    MessageBox.Show("this is pool thread.");
                }
                else
                {
                    MessageBox.Show("this is not pool thread.");
                }
                back.RunWorkerAsync();
            }
            catch
            {
                btnStart.Enabled = true;
                btnCancel.Enabled = false;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            try
            {
                btnStart.Enabled = true;
                btnCancel.Enabled = false;
                back.CancelAsync();
            }
            catch
            {
                btnStart.Enabled = false;
                btnCancel.Enabled = true;
            }
        }

        private void back_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            progressBar1.Value = e.ProgressPercentage;
        }

        private void back_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (e.Cancelled)
            {
                MessageBox.Show("Cancelled");                
            }
            else
            {
                MessageBox.Show("Finished");
            }
            this.progressBar1.Value = 0;
        }

        private void back_DoWork(object sender, DoWorkEventArgs e)
        {
            if (Thread.CurrentThread.IsThreadPoolThread)
            {
                MessageBox.Show("this is pool thread.");
            }
            else
            {
                MessageBox.Show("this is not pool thread.");
            }
            int sum = 100000000;
            int percent = sum / 100;
            for (int i = 1; i < sum; i++)
            {
                if (back.CancellationPending)
                {
                    e.Cancel = true;
                    return;
                }
                else if (i % percent == 0)
                {
                    back.ReportProgress(i / percent);
                }
            }

        }

    }
}
